package domain.worm;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.events.constants.Key;

import domain.components.Bullet;
import domain.components.SightComponent;


public class PointingState extends WormState {

	private SightComponent sight;
	private double potencia = 0;
	private double movX, movY;
	
	public PointingState(Worm worm) {
		super(worm);
		
		if(this.getWorm().getDir() == 1)
			this.getWorm().setAppearance(this.getWorm().getBazRightAnimation());
		else
			this.getWorm().setAppearance(this.getWorm().getBazLeftAnimation());
		
		this.movX = 0;
		this.movY = -1;
		
		this.sight = new SightComponent(this.getWorm().getX()+ this.getWorm().getDiameter()/2,
										this.getWorm().getY()- this.getWorm().getDiameter()/2, this.getWorm());
		this.getWorm().getScene().addComponent(this.sight);
	}

	public void update(DeltaState deltaState) {		
		this.cambiarAStandBy(deltaState);
		
		if(deltaState.isKeyBeingHold(Key.SPACE)){
			//creo la bala con la posicion x e y de el componente sight
			System.out.println("estoy apretando");
			this.potencia = this.potencia + 0.01;
		}
		if(deltaState.isKeyReleased(Key.SPACE)){
			System.out.println("estoy soltando");
			Bullet bala = new Bullet(15, 10, this.sight.getX(), this.sight.getY(), this.potencia);
			this.getWorm().getScene().addComponent(bala);
			this.potencia = 0;
			}
		
	}
	
	

	private void cambiarAStandBy(DeltaState deltaState){
		if (deltaState.isKeyBeingHold(Key.RIGHT) | deltaState.isKeyPressed(Key.RIGHT)){
			this.sight.destroy();
			this.getWorm().setState(new StandbyState(this.getWorm()));
		}
		if (deltaState.isKeyBeingHold(Key.LEFT) | deltaState.isKeyPressed(Key.LEFT)){
			this.sight.destroy();
			this.getWorm().setState(new StandbyState(this.getWorm()));
		}
	}

}
